//
//  R4Controller.m
//  TheRicher
//
//  Created by 李晓东 on 2018/2/5.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "R14Controller.h"
#import "R1Cell.h"

@interface R14Controller () <DatabaseResultDelegate>

@end

@implementation R14Controller

- (void)loadView{
    [super loadView];
    
    self.dataArray = [NSMutableArray array];
    self.netService = [NetService share];
    self.databaseService = [DatabaseService share];
    
    [self.tableView setDataSource:self];
    [self.tableView registerClass:[R1Cell class] forCellReuseIdentifier:@"TableCellId"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.先查询数据库
    self.databaseService.delegate = self;
    self.tableName = ShuangSeQiuTableName;
    [self.databaseService queryTable:ShuangSeQiuTableName withCondition:nil ofConditionName:nil andJudgeFlag:nil andOrderBy:@"issueno" andSortBy:1 andCountOf:RowCount + 1];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    R1Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCellId" forIndexPath:indexPath];
    [cell setValues:self.dataArray[indexPath.row]];
    [cell setSelectedBackgroundView:[[UIView alloc]initWithFrame:cell.bounds ]];
    [cell.selectedBackgroundView setBackgroundColor:[UIColor redColor]];
    
    return cell;
}

- (void)resultInDatabase:(NSArray *)resultArray{
    [[KillNumberTools share] type14:resultArray];
    resultArray = [resultArray subarrayWithRange:NSMakeRange(0, resultArray.count - 1)];

    if ([self.dataArray count] > 0) {
        NSArray *tempArray = [[(ShuangSeQiu *)resultArray[0] killingNumber]componentsSeparatedByString:@","];
        NSMutableString *deadthNumber = [NSMutableString string];
        if ([[(ShuangSeQiu *)self.dataArray.lastObject number] containsString:tempArray[0]]) {
            [deadthNumber appendString:tempArray[0]];
        }
        if ([[(ShuangSeQiu *)self.dataArray.lastObject number] containsString:tempArray[1]]) {
            [deadthNumber appendFormat:@", %@", tempArray[1]];
        }
        [(ShuangSeQiu *)self.dataArray.lastObject setDeadthNumber:deadthNumber];
    }
    for (int i = 0; i < resultArray.count - 1; i++ ) {
        NSArray *tempArray = [[(ShuangSeQiu *)resultArray[i+1] killingNumber]componentsSeparatedByString:@","];
        NSMutableString *deadthNumber = [NSMutableString string];
        
        if ([[(ShuangSeQiu *)resultArray[i] number] containsString:tempArray[0]]) {
            [deadthNumber appendString:tempArray[0]];
        }
        if ([[(ShuangSeQiu *)resultArray[i] number] containsString:tempArray[1]] ) {
            [deadthNumber appendFormat:deadthNumber.length > 0 ? @", %@" : @"%@", tempArray[1]];
        }
        [(ShuangSeQiu *)resultArray[i] setDeadthNumber:deadthNumber];

    }
    
    [self.dataArray addObjectsFromArray:resultArray];
    [self.tableView reloadData];
    
    if ([self.activityIndicatorView isAnimating]) {
        [self.activityIndicatorView stopAnimating];
    }
}

- (void)typeMethodInKillNumberTools:(NSArray *)resultArray{
//    [[KillNumberTools share]type14:resultArray];
}


- (void)judgeKillingNumber:(NSInteger)resultCount{
    NSArray *processData = [[KillNumberTools share]type14:[self.dataArray subarrayWithRange:NSMakeRange(0, resultCount + 1)]];
    processData = [processData subarrayWithRange:NSMakeRange(0, resultCount)];
    [self.dataArray replaceObjectsInRange:NSMakeRange(0, resultCount) withObjectsFromArray:processData];
    for (int i = 0; i < resultCount; i++) {
        
        NSArray *tempArray = [[(ShuangSeQiu *)self.dataArray[i+1] killingNumber] componentsSeparatedByString:@","];
        NSMutableString *deadthNumber = [NSMutableString string];
        if([(ShuangSeQiu *)self.dataArray[i] judgeWhetherItContain:tempArray[0]]) {
            [deadthNumber appendString:tempArray[0]];
        }
        if( [(ShuangSeQiu *)self.dataArray[i] judgeWhetherItContain:tempArray[1]]) {
            [deadthNumber appendFormat:deadthNumber.length > 0 ? @", %@" : @"%@", tempArray[1]];
        }
        [(ShuangSeQiu *)self.dataArray[i] setDeadthNumber:deadthNumber];
    }
    self.needToRefreshCount = 0;
    [self.tableView reloadData];
}

- (void)refreshChildControllerView:(NSInteger)refreshCount{
    [self judgeKillingNumber:refreshCount];
}

- (void)resultNeedToInsertKillingDatabase:(NSArray *)insertArray{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[DatabaseService share] insertTableOfKillingNumberWithObjects:insertArray inColumn:@"type14"];
        
    });
}
@end

